<template>
  <div class="app-container">
 
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house_sale:earnestmoney:add']"
        >新增</el-button>
      </el-col>
    
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house_sale:earnestmoney:remove']"
        >删除</el-button>
      </el-col>
    
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="earnestmoneyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="earnestid" /> -->
      <!-- <el-table-column label="购买房产编号" align="center" prop="buyid" /> -->
      <el-table-column label="客户名称" align="center" prop="custname" />
      <el-table-column label="房产名称" align="center" prop="housename" />
      
      <el-table-column label="票据本" align="center" prop="ticketname" />
      <el-table-column label="票据本编号" align="center" prop="ticketflow" />
      <el-table-column label="收款方式" align="center" prop="paidtypeid" />
      <el-table-column label="诚意金" align="center" prop="paidmoney" />
      <el-table-column label="付款时间" align="center" prop="paidtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paidtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否转收付款" align="center" prop="istranslate" />
      <el-table-column label="转收付款金额" align="center" prop="transmoney" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="操作人" align="center" prop="userid" />
      <el-table-column label="录入时间" align="center" prop="oprtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oprtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否有效" align="center" prop="invalid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === '收取'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:earnestmoney:edit']"
          >转签约</el-button>
           <el-button v-if="scope.row.status === '转签约'"
            size="mini"
            type="text"
            icon="el-icon-edit"
          >已转签约</el-button>

          <el-button  v-if="scope.row.invalid === '有效'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate2(scope.row)"
          >作废</el-button>
           <el-button  v-if="scope.row.invalid === '作废'"
            size="mini"
            type="text"
            icon="el-icon-delete"
          >已作废</el-button>
        </template>
          <!-- v-hasPermi="['house_sale:earnestmoney:remove']" -->
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改诚意金管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" >
        <el-form-item label="购买房产编号" prop="buyid" hidden>
          <el-input v-model="form.buyid"  />
        </el-form-item>
        <el-form-item label="意向客户" prop="custname">
          <el-input v-model="form.custname"  disabled/>
        </el-form-item>
          <el-form-item label="意向房产" prop="housename">
          <el-input v-model="form.housename"  disabled/>
        </el-form-item>
        <el-form-item label="票据本" prop="ticketno">
           <el-select v-model="form.ticketno"  placeholder="请选择票据本" @change="getTicket" :style="{width:'100%'}">
                 <el-option
                  v-for="item in ticketList"
                  :key="item.ticketid"
                  :label="item.ticketno"
                  :value="item.ticketid"
                  :disabled="false"
                ></el-option> 
            </el-select>  
          <!-- <el-input v-model="form.ticketno" placeholder="请输入票据本" /> -->
        </el-form-item>
        <el-form-item label="票据本编号" prop="ticketflow">
          <el-input v-model="form.ticketflow" disabled/>
        </el-form-item>
        <el-form-item label="收款方式" prop="paidtypeid">
           <el-select v-model="form.paidtypeid"  placeholder="请选择收款方式" :style="{width:'100%'}">
                 <el-option
                  v-for="item in paidtypeList"
                  :key="item.paidtypeid"
                  :label="item.paidtypename"
                  :value="item.paidtypename"
                  :disabled="false"
                ></el-option> 
            </el-select>  
          <!-- <el-input v-model="form.paidtypeid" placeholder="请输入收款方式）" /> -->
        </el-form-item>
        <el-form-item label="诚意金" prop="paidmoney">
          <el-input v-model="form.paidmoney" placeholder="请输入诚意金"/>
        </el-form-item>
        <el-form-item label="付款时间" prop="paidtime" >
          <el-date-picker clearable :style="{width:'100%'}"
            v-model="form.paidtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择付款时间"
            @change="getTime">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否转收付款" prop="istranslate" hidden>
          <el-input v-model="form.istranslate" placeholder="请输入是否转收付款" />
        </el-form-item>
        <el-form-item label="转收付款金额" prop="transmoney" hidden>
          <el-input v-model="form.transmoney" disabled/>
        </el-form-item>
        <el-form-item label="操作人" prop="userid" hidden>
          <el-input v-model="form.userid" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="录入时间" prop="oprtime" hidden>
          <el-date-picker clearable
            v-model="form.oprtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否有效" prop="invalid" hidden>
          <el-input v-model="form.invalid" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listEarnestmoney, getPaidType, getEarnestmoney,toContract,beInvalid, delEarnestmoney, addEarnestmoney, updateEarnestmoney,addFinance } from "@/api/house_sale/earnestmoney";
import { updNextNo}from "@/api/house_sale/ticket";
export default {
  name: "Earnestmoney",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 诚意金管理表格数据
      earnestmoneyList: [],
      paidtypeList: [],
      ticketList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buyid: null,
        ticketno: null,
        ticketflow: null,
        paidtypeid: null,
        paidmoney: null,
        paidtime: null,
        istranslate: null,
        transmoney: null,
        status: null,
        userid: null,
        oprtime: null,
        invalid: null
      },
      // 表单参数
      form: {},
      finance: {
        buyid: null,
        ticketno: null,
        ticketflow: null,
        paidtypeid: null,
        paidmoney: null,
        paidperson: null,
        paidsortid: null,
       
        paidtime: null,
        paidintime: null,
        invalid: null,
        userid: null,
        oprtime: null,
        oprtype: null
      },
      // 表单校验
      rules: {
        buyid: [
          { required: true, message: "购买房产编号不能为空", trigger: "blur" }
        ],
        ticketno: [
          { required: true, message: "票据本不能为空", trigger: "blur" }
        ],
        ticketflow: [
          { required: true, message: "票据本编号不能为空", trigger: "blur" }
        ],
        paidtypeid: [
          { required: true, message: "收款方式类别", trigger: "blur" }
        ],
        paidmoney: [
          { required: true, message: "诚意金不能为空", trigger: "blur" }
        ],
        paidtime: [
          { required: true, message: "付款时间不能为空", trigger: "blur" }
        ],
        istranslate: [
          { required: true, message: "是否转收付款不能为空", trigger: "blur" }
        ],
        transmoney: [
          { required: true, message: "转收付款金额不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        userid: [
          { required: true, message: "操作人不能为空", trigger: "blur" }
        ],
       
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
   /**自动获取票据 */
    getTicket(temp){
       this.ticketList.forEach(element => {
        if(element.ticketid === temp){

          if(element.nextno<10){
            this.form.ticketflow=element.prefix+"00"+element.nextno;
          }else if(10<=element.nextno<100){
            this.form.ticketflow=element.prefix+"0"+element.nextno;
          }else{
            this.form.ticketflow=element.prefix+element.nextno;
          }
          
        }
      });
    },
  
    /**自动获取录入时间 */
    getTime(){
      if(this.form.paidtime!=null){
        this.form.oprtime= this.form.paidtime;
      }
    },
    /** 查询诚意金管理列表 */
    getList() {
      this.loading = true;
      const id=this.$route.params.buyid;
   
      listEarnestmoney(id).then(response => {
        this.earnestmoneyList = response.rows;
        //  this.earnestmoneyList[0].custname=this.$route.query.custname;
        //  this.earnestmoneyList[0].housename=this.$route.query.housename;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        earnestid: null,
        buyid: this.$route.params.buyid,
        custname: this.$route.query.custname,
        housename: this.$route.query.housename,
        ticketno: 1,
        ticketflow: null,
        paidtypeid: "现金",
        paidmoney: null,
        paidtime: null,
        istranslate: "否",
        transmoney: "0",
        status: "收取",
        userid: this.$store.state.user.name,
        oprtime: null,
        invalid: "有效"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.earnestid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
        getPaidType().then(response => {
        this.paidtypeList = response.paidtypeList;
        this.ticketList = response.ticketList;
        this.open = true;
        this.title = "添加诚意金管理";
      });
    
    },
    /** 转签约按钮操作 */
    handleUpdate(row) {
      // this.reset();
      const earnestid = row.earnestid 
      this.finance.buyid=row.buyid
      this.finance.ticketno=row.ticketno
      this.finance.ticketflow=row.ticketflow
      this.finance.paidtypeid=row.paidtypeid
      this.finance.paidmoney=row.paidmoney
      this.finance.paidperson=row.custname
      this.finance.paidsortid="转签约"
      this.finance.paidtime=row.paidtime
      this.finance.paidintime=row.paidtime
      this.finance.oprtime=row.oprtime
      this.finance.invalid=row.invalid
      this.finance.userid=row.userid
      this.finance.oprtype="收款"
     
      getEarnestmoney(earnestid).then(response => {
        var form1 = response.data;
        form1.istranslate="是";
        form1.transmoney=response.data.paidmoney;
        form1.status="转签约"

        this.$modal.confirm('是否确认转签约').then(function() {
         return toContract(form1);//去转签约
      }).then(() => {
        addFinance(this.finance);
        this.getList();
        this.$modal.msgSuccess("转签约成功");
      }).catch(() => {});
      });

    },
    /**作废按钮操作 */
    handleUpdate2(row){
      const earnestid = row.earnestid 
      getEarnestmoney(earnestid).then(response => {
        var form2 = response.data;
        form2.invalid="作废";

        this.$modal.confirm('是否确认作废').then(function() {
         return beInvalid(form2);//去作废
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("已作废");
      }).catch(() => {});
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.earnestid != null) {
            updateEarnestmoney(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
           updNextNo(this.form.ticketno).then(response => {
            // this.$modal.msgSuccess("修改成功");
          
            });
            addEarnestmoney(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const earnestids = row.earnestid || this.ids;
      this.$modal.confirm('是否确认删除诚意金管理编号为"' + earnestids + '"的数据项？').then(function() {
        return delEarnestmoney(earnestids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/earnestmoney/export', {
        ...this.queryParams
      }, `earnestmoney_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
